/*
 * @Author: pandaman
 * @Date: 2024-02-20 12:15:56
 * @LastEditors: 姚潘涛
 * @LastEditTime: 2024-10-10 16:37:44
 * @FilePath:
 * /xict_ws/src/xcmg-intelligent-cockpit-three/src/xict_cpp/src/xict_driver/param/drivers_params.cc
 * @Description:
 *
 * Copyright (c) 2024 by pandaman, All Rights Reserved.
 */
#include "xict_driver/param/drivers_params.h"

namespace xict_driver {
    namespace param {
        xict_driver::utils::SerialParity
        StrToParity(const std::string& _parity) {
            if (_parity == "NO") {
                return xict_driver::utils::SerialParity::NO_PARITY;
            } else if (_parity == "EVEN") {
                return xict_driver::utils::SerialParity::EVEN_PARITY;
            } else if (_parity == "ODD") {
                return xict_driver::utils::SerialParity::ODD_PARITY;
            }

            return xict_driver::utils::SerialParity::NO_PARITY;
        }

        xict_driver::utils::CameraDriverConfig
        DriversParams::LoadCameraDriverConfig(const YAML::Node& _yaml_node) {
            xict_driver::utils::CameraDriverConfig config;
            xict_common::utils::YamlReadAbort<bool>(_yaml_node, "enable",
                                                    config.enable);
            xict_common::utils::YamlReadAbort<std::string>(_yaml_node, "name",
                                                           config.name);
            xict_common::utils::YamlReadAbort<double>(_yaml_node, "period",
                                                      config.period);
            // xict_common::utils::YamlReadAbort<std::string>(_yaml_node,
            // "device", config.device);
            xict_common::utils::YamlReadAbort<int>(_yaml_node, "port",
                                                   config.port);
            xict_common::utils::YamlReadAbort<std::string>(_yaml_node, "topic",
                                                           config.topic);
            xict_common::utils::YamlReadAbort<bool>(_yaml_node, "rtsp_enable",
                                                    config.rtsp_enable);
            xict_common::utils::YamlReadAbort<std::string>(
                _yaml_node, "rtsp_url", config.rtsp_url);

            return config;
        }

        DriversParams::DriversParams() = default;

        void DriversParams::Load(std::string _path_of_yaml) {
            YAML::Node config;
            try {
                config = YAML::LoadFile(_path_of_yaml);
            } catch (...) {
                AERROR_F(
                    "The format of config file {} is wrong, Please check (e.g. "
                    "indentation).",
                    _path_of_yaml.c_str());
                return;
            }

            // Read sensor driver Params
            YAML::Node sensor_driver_config =
                xict_common::utils::YamlSubNodeAbort(config, "sensor_driver");
            for (auto&& i : sensor_driver_config) {
                bool config_enable_flag = i["enable"].as<bool>();

                if (!config_enable_flag)
                    continue;

                xict_driver::utils::DriverType driver_type =
                    xict_driver::utils::StrToDriverType(
                        i["type"].as<std::string>());
                xict_driver::utils::SensorType sensor_type =
                    xict_driver::utils::DriverType2SensorType(driver_type);
                if (sensor_type == xict_driver::utils::SensorType::CAMERA) {
                    xict_driver::utils::CameraDriverConfig camera_config =
                        LoadCameraDriverConfig(i);
                    camera_config.type = driver_type;
                    camera_driver_configs_.push_back(camera_config);
                    // Print();
                } else {
                    AERROR_F(
                        "[DriversParams] The type of sensor driver is wrong!");
                }
            }
        }

        void DriversParams::Print() {
            std::cout << "\033[1;32m----> Driver Params <----\033[0m"
                      << std::endl;

            for (auto& camera_driver_config : camera_driver_configs_) {
                PrintLine("Driver Name", camera_driver_config.name);
                PrintLine("Driver Type", xict_driver::utils::DriverTypeToStr(
                                             camera_driver_config.type));
                // PrintLine("Driver Device", camera_driver_config.device);
                PrintLine("Driver Port", camera_driver_config.port);
                PrintLine("Driver Topic", camera_driver_config.topic);
                PrintLine("Driver Period", camera_driver_config.period);

                std::cout << BLUE << "--------------------------" << RESET
                          << std::endl;
            }

            std::cout << "\033[1;32m--------------------------\033[0m"
                      << std::endl;

            std::cout << std::endl;
        }
    }   // namespace param
}   // namespace xict_driver